import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN

# 创建一个 12x12 的方格坐标图
fig, ax = plt.subplots(figsize=(8, 8))
ax.set_xlim([0, 12])
ax.set_ylim([0, 12])

# 绘制横向和纵向的线条
for i in range(13):
    ax.axhline(y=i, xmin=0, xmax=12, color='black', linestyle='-')
    ax.axvline(x=i, ymin=0, ymax=12, color='black', linestyle='-')

# 生成随机样本数据
np.random.seed(0)
X = np.random.rand(100, 2) * 12

# 使用DBSCAN聚类算法
dbscan = DBSCAN(eps=1, min_samples=5)
labels = dbscan.fit_predict(X)

# 绘制聚类结果
colors = ['red', 'blue', 'green', 'orange', 'purple']
for label in set(labels):
    if label == -1:
        # 将噪声点标记为黑色
        color = 'black'
    else:
        color = colors[label % len(colors)]
    ax.scatter(X[labels == label, 0], X[labels == label, 1], c=color, label=label)

plt.legend()
plt.show()
